/// Edit Deck Panel ///

using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.Threading;
using ajma.Utils;
using System;
using System.IO;
using System.Collections;

namespace Xyne
{
	public class EditDeckPanel : Panel
	{
		private const int LIST_WIDTH = 150;
		private const int LIST_HEIGHT = 250;
		private const int INFO_HEIGHT = 140;
		
		private Button cancel;
		private Button save;
		private Label deckLabel;
		private Label cardLabel;
		private Button addButton;
		private Button removeButton;
		private ListBox deckContents;
		private ListBox possibleCards;
		private Label info;
		private ButtonClicked backCallback;
		
		private string filename;
		private string deckname;
		
		public delegate void ButtonClicked();
		
		public ButtonClicked BackCallback
		{
			get { return backCallback; }
			set { backCallback = value; }
		}
		
		public EditDeckPanel()
		{
			ConstructControls();
			
			foreach( Pack pack in CardRepository.GetRepository().Packs )
			{
				foreach( Card card in pack.Cards )
				{
					possibleCards.Items.Add(card.Name);
				}
			}
		}
		
		public void LoadDeck( string filename )
		{
			this.filename = filename;
			
			FileInfo file = new FileInfo(filename);
			StreamReader reader = file.OpenText();
					
			deckname = reader.ReadLine();
			
			string line;
			while( (line = reader.ReadLine()) != null )
			{
				deckContents.Items.Add(line);
			}
					
			reader.Close();
		}
		
		private void ConstructControls()
		{
			SuspendLayout();
			
			deckLabel = new Label();
			deckLabel.Text = "Deck";
			deckLabel.Location = new Point(5,5);
			Controls.Add(deckLabel);
			
			deckContents = new ListBox();
			deckContents.HorizontalScrollbar = true;
			deckContents.Location = new Point(5,deckLabel.Location.Y + deckLabel.Height);
			deckContents.Size = new Size(LIST_WIDTH,LIST_HEIGHT);
			deckContents.Click += new EventHandler(CardListClicked);
			Controls.Add(deckContents);
			
			addButton = new Button();
			addButton.Text = "<--";
			addButton.Location = new Point(deckContents.Location.X + deckContents.Width + 5,
			                               deckContents.Location.Y + LIST_HEIGHT / 3);
			addButton.Click += new EventHandler(MoveClicked);
			Controls.Add(addButton);
			
			removeButton = new Button();
			removeButton.Text = "Remove";
			removeButton.Location = new Point(deckContents.Location.X + deckContents.Width + 5,
			                               deckContents.Location.Y + LIST_HEIGHT * 2 / 3);
			removeButton.Click += new EventHandler(RemoveClicked);
			Controls.Add(removeButton);
			
			cardLabel = new Label();
			cardLabel.Text = "Cards";
			cardLabel.Location = new Point(removeButton.Location.X + removeButton.Width + 5,5);
			Controls.Add(cardLabel);
			
			possibleCards = new ListBox();
			possibleCards.HorizontalScrollbar = true;
			possibleCards.Location = new Point(cardLabel.Location.X,cardLabel.Location.Y + deckLabel.Height);
			possibleCards.Size = new Size(LIST_WIDTH,LIST_HEIGHT);
			possibleCards.Click += new EventHandler(CardListClicked);
			Controls.Add(possibleCards);
			
			int width = possibleCards.Location.X + possibleCards.Width + 10;
			
			info = new Label();
			info.Location = new Point(5,deckContents.Location.Y + deckContents.Height + 5);
			info.Size = new Size(width - 20,INFO_HEIGHT);
			Controls.Add(info);
			
			int height = info.Location.Y + info.Height + 5;
			
			cancel = new Button();
			cancel.Text = "Cancel";
			cancel.Location = new Point(width - 15 - cancel.Size.Width,height);
			cancel.Click += new EventHandler(CancelClicked);
			Controls.Add(cancel);
			
			save = new Button();
			save.Text = "Save";
			save.Location = new Point(cancel.Location.X - save.Size.Width - 5,height);
			save.Click += new EventHandler(SaveClicked);
			Controls.Add(save);
			
			height += cancel.Size.Height + 30;
			
			ClientSize = new Size(width,height);
			
			ResumeLayout();
		}
		
		private void CancelClicked(object sender, EventArgs e)
		{
			backCallback();
		}
		
		private void SaveClicked(object sender, EventArgs e)
		{
			FileInfo file = new FileInfo(filename);
			StreamWriter write = file.CreateText();
			
			write.WriteLine(deckname);
			
			foreach( Object item in deckContents.Items )
			{
				write.WriteLine(item.ToString());
			}
			
			write.Close();
			
			CancelClicked(sender, e);
		}
		
		private void MoveClicked(object sender, EventArgs e)
		{
			if( possibleCards.SelectedItem != null )
			{
				deckContents.Items.Add(possibleCards.SelectedItem);
			}
		}
		
		private void RemoveClicked(object sender, EventArgs e)
		{
			if( deckContents.SelectedItem != null )
			{
				deckContents.Items.Remove(deckContents.SelectedItem);
			}
		}
			
		private void CardListClicked(object sender, EventArgs e)
		{
			ListBox box = (ListBox)sender;
			if( box.SelectedItem != null )
			{
				foreach( Pack pack in CardRepository.GetRepository().Packs )
				{
					foreach( Card card in pack.Cards )
					{
						if( card.Name == box.SelectedItem.ToString() )
						{
							string text = card.Name + "\n\nEffects\n";
							foreach( Effect effect in card.Effects )
							{
								text += effect.ShortText + " (" + effect.LongText + ")\n";
							}
							text += "\n";
							text += card.Description;
							info.Text = text;
							return;
						}
					}
				}
			}
		}
	}
}